/**
* Created by yyr on 2018/3/16
*已投资基金选项卡合伙人认缴
*/
<template>
    <div>
        <div class="ca-box-border-top-none ca-p-22">
            <div class="ca-title">
                <h3>合伙人认缴信息</h3>
                <div class="ca-pull-right ca-p-b-20">
                    <el-button  type="primary" class="ca-m-l-10" @click="addPartner" :disabled="!isAuthPartnerEdit">
                        添加
                    </el-button>
                </div>
            </div>
            <!--列表-->
            <div class="ca-m-t-20">
                <div v-for="(row,index) in fundPartner"  :key="index" :class="[{'ca-m-t-30':index>0}]">
                    <div class="ca-box ca-p-20">
                        <div class="ca-title" v-if="!row.editStatus">
                            <h3>合伙人认缴信息</h3>
                            <div class="ca-pull-left ca-m-l-20" style="line-height:36px;font-size:14px;color:#666">
                                {{$moment( row.fundPartnerEntity.recordingTime ).format("YYYY-MM-DD")}}
                            </div>
                            <div class="ca-pull-right">
                                <button type="button" @click="partnerOpen(index)" class="ca-btn-switch ca-btn-default ca-m-l-10" v-if="row.show"><i class="el-icon-arrow-up ca-m-r-5"></i><span>收起</span></button>
                                <button type="button" @click="partnerOpen(index)" class="ca-btn-switch ca-btn-default ca-m-l-10" v-else><i class="el-icon-arrow-down ca-m-r-5"></i><span>展开</span></button>
                                <el-button type="text" @click="editPartnerItem(row,index)" :disabled='!isAuthPartnerEdit' class="ca-edit-btn el-icon-edit ca-m-l-20">编辑</el-button>
                            </div>
                        </div>
                        <div class="ca-m-t-5" v-if="row.show">
                            <div style="line-height:20px;">
                                <div style="float: left;color:#999;font-size:14px">变更原因：</div><div style="margin-left:68px;color:#666666;font-size:14px;">{{row.fundPartnerEntity.reason}}</div>
                            </div>
                            <el-table
                                :data="row.distHistoryList"
                                stripe
                                border
                                style="width:100%" class="table_p_01 ca-m-t-15 ca-table">
                                <el-table-column
                                    show-overflow-tooltip
                                    label="合伙人类型">
                                    <template slot-scope="scope">
                                        {{scope.row.type | csConstFilter('PARTNER_TYPE')}}
                                    </template>
                                </el-table-column>
                                <el-table-column
                                    label="数据来源">
                                    <template slot-scope="scope">
                                        {{scope.row.partnerType | getNameById('lpSource')}}
                                    </template>
                                </el-table-column>
                                <el-table-column
                                    show-overflow-tooltip
                                    prop="customerName"
                                    label="合伙人名称">
                                    <template slot-scope="scope">
                                        <el-button
                                            type="text"
                                            @click="goPartnerDetail(scope.row.customerId,scope.row.partnerType)"
                                            :disabled="disableFunction(scope.row)">{{scope.row.customerName}}
                                        </el-button>
                                    </template>
                                </el-table-column>
                                <el-table-column
                                    show-overflow-tooltip
                                    prop="stages"
                                    label="出资分期">
                                    <template slot-scope="scope">
                                        {{scope.row.stages}}
                                    </template>
                                </el-table-column>
                                <el-table-column
                                    show-overflow-tooltip
                                    prop="life"
                                    label="缴纳年限">
                                    <template slot-scope="scope">
                                        {{scope.row.life}}
                                    </template>
                                </el-table-column>
                                <el-table-column
                                    prop="subscribed"
                                    label="认缴金额(万元)">
                                    <template slot-scope="scope">
                                        {{scope.row.subscribed | format1}}
                                    </template>
                                </el-table-column>
                                <el-table-column
                                    prop="paid"
                                    label="实缴金额(万元)">
                                    <template slot-scope="scope">
                                        {{scope.row.paid | format1}}
                                    </template>
                                </el-table-column>
                                <el-table-column
                                    prop="proportion"
                                    label="当前权益(%)">
                                    <template slot-scope="scope">
                                        {{scope.row.proportion}}
                                    </template>
                                </el-table-column>
                                <el-table-column
                                    label="持有状态">
                                    <template slot-scope="scope">
                                        {{scope.row.holdingState | getNameById('fundHolding')}}
                                    </template>
                                </el-table-column>
                            </el-table>
                            <el-row class="ca-title ca-m-t-8">
                                <h3>附件</h3>
                            </el-row>
                            <div class="ca-file-ul">
                                <el-row class="ca-file-row" :gutter="20" v-for="(file,fileIndex) in row.fileList" :key="fileIndex">
                                    <el-col :span="4" class="upload-time">{{file.uploadTime|moment('YYYY-MM-DD')}}</el-col>
                                    <el-col :span="1">
                                        <tz-icon-file-type class="ca-m-r-5" :fileType="file.fileName"></tz-icon-file-type>
                                    </el-col>
                                    <el-col :span="8">
                                        <div class="ca-text-over-flow-ellipsis ca-pointer"
                                             @click="previewFileCheck(file.id, file.fileName)">
                                            <span class="ca-font-size-16 ca-pointer">{{file.fileName}}</span>
                                        </div>
                                    </el-col>
                                    <el-col :span="4">
                                        <div class="ca-text-over-flow-ellipsis">{{userName}}</div>
                                    </el-col>
                                    <el-col :span="3" class="ca-pull-right ca-align-right">
                                        <a class="ca-download ca-m-r-10" @click="downloadFile(file.id)"></a>
                                    </el-col>
                                </el-row>
                                <div v-if="!fileDetail" class="ca-font-color-999">暂无数据</div>
                            </div>
                        </div>
                        <csPartnerEdit
                            :editData="editData"
                            :editIndex="editIndex"
                            :isMotherFund="isMotherFund"
                            @cancelEdit="cancelEdit"
                            v-if="fundPartner[index].editStatus"
                            @addRedDot="addRedDot"
                            @refresh="refresh"
                        ></csPartnerEdit>
                    </div>
                </div>
            </div>
        </div>
        <!--添加弹出框-->
        <el-dialog title="" :visible.sync="dialogFormVisible" width="80%"
                   :before-close="cancelDialog" :show-close="false">
            <div class="ca-m-t-0">
                <div class="ca-title">
                    <h3>合伙人认缴信息</h3>
                    <div class="ca-pull-left ca-m-l-20">
                        <el-date-picker type="date"  v-model='partnerListAdd.fundPartnerEntity.recordingTime' placeholder="选择日期" style="width: 150px;"></el-date-picker>
                    </div>
                    <div class='ca-align-right'>
                        <el-button type="primary" @click="addPartnerData()">保 存</el-button>
                        <el-button @click="cancelDialog()">取 消</el-button>
                    </div>
                </div>
                <div>
                    <el-row class="ca-m-t-15">
                        <el-col :span="2">变更原因</el-col>
                        <el-col :span="22">
                            <el-input  v-model='partnerListAdd.fundPartnerEntity.reason' type="textarea" :maxlength="400" placeholder="请输入内容"></el-input>
                        </el-col>
                    </el-row>
                    <table class="ca-table ca-m-t-15">
                        <tr>
                            <th width="10%">合伙人类型</th>
                            <th width="10%">数据来源</th>
                            <th width="10%">合伙人名称</th>
                            <th width="10%">出资分期</th>
                            <th width="10%">缴纳年限</th>
                            <th width="10%">认缴金额(万元)</th>
                            <th width="10%">实缴金额(万元)</th>
                            <th width="10%">持有状态</th>
                            <th width="10%" class="p-tb-0"><button type="button" class="ca-btn ca-btn-round" @click="addPartnerTableData()"><i class="fa fa-plus"></i></button></th>
                        </tr>
                        <tr v-for="(list,index) in partnerListAdd.distHistoryList" :key="index">
                            <td>
                                <el-select v-model="list.type" placeholder="请选择" style="width:100%">
                                    <el-option
                                        v-for="item in initView.type"
                                        :label="item.name"
                                        :key="item.id"
                                        :value="item.id">
                                    </el-option>
                                </el-select>
                            </td>
                            <td>
                                <el-select v-model="list.partnerType"  @change="lpSourceChange(index)" placeholder="请选择" style="width:100%">
                                    <el-option
                                        v-for="item in initView.lpSource"
                                        :key="item.id"
                                        :label="item.name"
                                        :value="item.id"
                                    ></el-option>
                                </el-select>
                            </td>
                            <td>
                                <el-select v-model="list.customerId"
                                           @change="changePartner(index)"
                                           filterable placeholder="请选择" style="width:100%;">
                                    <el-option
                                        v-for="item in list.userSourceArr"
                                        :key="item.id"
                                        :label="item.customerName"
                                        :value="item.id">
                                    </el-option>
                                </el-select>
                            </td>
                            <td><el-input placeholder="请输入内容" type="number" v-model="list.stages" class="ca-vertical-align-middle"></el-input></td>
                            <td><el-input v-model="list.life" type="number" placeholder="请输入内容" class="ca-vertical-align-middle"></el-input></td>
                            <td><el-input v-model="list.subscribed" type="number" placeholder="请输入内容" class="ca-vertical-align-middle"></el-input></td>
                            <td><el-input v-model="list.paid" placeholder="请输入内容" type="number"  class="ca-vertical-align-middle"></el-input></td>
                            <td>
                                <el-select v-model="list.holdingState" placeholder="请选择" style="width:100%">
                                    <el-option
                                        v-for="item in initView.fundHolding"
                                        :label="item.name"
                                        :key="item.id"
                                        :value="item.id">
                                    </el-option>
                                </el-select>
                            </td>
                            <td class="p-tb-0">
                                <button type="button" class="ca-btn ca-btn-round" @click="deletePartnerSingle(list,-1)"><i class="fa fa-remove"></i></button>
                            </td>
                        </tr>
                    </table>
                </div>
                <el-row class="ca-title ca-m-15">
                    <h3>附件</h3>
                    <div class="ca-align-right">
                        <csUpload
                            :fileType="fileType"
                            :businessId="itemId"
                            uploadType="all"
                            :getFileUploadedReturnData="getFileUploadedReturnDataFun"
                        ></csUpload>
                    </div>
                </el-row>
                <div class="ca-file-ul">
                    <el-row class="ca-file-row" :gutter="20" v-for="(file, fileIndex) in fileDetail" :key="fileIndex">
                        <el-col :span="4" class="upload-time">{{file.uploadTime|moment('YYYY-MM-DD')}}</el-col>
                        <el-col :span="1">
                            <tz-icon-file-type class="ca-m-r-5" :fileType="file.fileName"></tz-icon-file-type>
                        </el-col>
                        <el-col :span="8">
                            <div class="ca-text-over-flow-ellipsis ca-pointer"
                                 @click="previewFileCheck(file.id, file.fileName)">
                                <span class="ca-font-size-16 ca-pointer">{{file.fileName}}</span>
                            </div>
                        </el-col>
                        <el-col :span="4">
                            <div class="ca-text-over-flow-ellipsis">{{userName}}</div>
                        </el-col>
                        <el-col :span="3" class="ca-pull-right ca-align-right">
                            <a class="ca-download ca-m-r-10" @click="downloadFile(file.id)"></a>
                            <el-button class="ca-table-btn" @click="deleteFileItem(fileIndex)">
                                <i class="fa fa-times"></i>
                            </el-button>
                        </el-col>
                    </el-row>
                    <div v-if="!fileDetail" class="ca-font-color-999">暂无数据</div>
                </div>
            </div>
        </el-dialog>
    </div>
</template>

<script type="text/ecmascript-6">
    import {api} from '../api';
    import {STATUS_HTTP_SUCCESS, apiHttp} from '__service/csHttp';
    import {isNoAuth} from '__service/utils';
    import mined from '__components/file-list/mixin';
    export default {
        mixins: [mined],
        data() {
            return {
                isAuthPartner: isNoAuth('客户管理-客户信息-查看'),
                isAuthMotherFund: isNoAuth('投资中母基金-母基金详情-查看'),
                isAuthPartnerEdit: true,
                itemId: this.$route.params.fundId,
                dialogFormVisible: false,
                fundPartner: [],
                editData: {},//编辑数据
                editIndex: 0,
                initView: {
                    type: this.$store.state.csConst.PARTNER_TYPE, //合伙人类型
                    fundHolding: this.$store.state.user.dictionaryMap.fundHolding,//持有状态
                    userOptions: this.$store.state.user.guest,//客户
                    lpSource: this.$store.state.user.dictionaryMap.lpSource,
                },
                partnerList: { //+号点击添加表格一行
                    attribute: '',  //资金属性
                    customerId: '', //合伙人Id
                    customerName: '',//合伙人name
                    holdingState: '',//持有状态 fundHolding
                    id: '',  //
                    life: '',//缴纳年限
                    paid: 0,//实缴金额
                    partnerId: '', //已设立基金合伙人主id
                    partnerType: null, //合伙人类型1 客户，2 母基金
                    stages: '', //出资分期
                    subscribed: '', //认缴金额
                    type: 0, //合伙人类型1 Lp 2 Gp
                    userSourceArr: [],
                },
                partnerListAdd: {   //保存时数据
                    distHistoryList: [{
                        attribute: '',  //资金属性
                        customerId: '', //合伙人Id
                        customerName: '',//合伙人name
                        holdingState: '',//持有状态 fundHolding
                        id: '',  //
                        life: '',//缴纳年限
                        paid: 0,//实缴金额
                        partnerId: '', //已设立基金合伙人主id
                        partnerType: null, //合伙人类型1 客户，2 母基金
                        stages: '', //出资分期
                        subscribed: '', //认缴金额
                        type: 0, //合伙人类型0Lp 1 Gp
                    }],
                    fundPartnerEntity: {
                        fundId: '',
                        id: '',
                        reason: '',
                        recordingTime: '',
                    },
                    fileList: [],//文件id
                },
                partnerName: '',
                customerId: '',
                fileType: '',//基金文件类型
                userSource: [],
                fileDetail: [],//上传成功文件返回的数据
                userName: this.$store.state.user.cnName,
            }
        },
        props: {
            fundId: {
                type: String,
                default() {
                    return ''
                }
            },
            isMotherFund: {
                type: Boolean,
                default() {
                    return false
                }
            },
            isAuth: {
                type: String,
                default() {
                    return '';
                }
            }
        },
        created() {
            if (this.isMotherFund === true) {
                this.itemId = this.fundId;
                this.initView.lpSource = [{id: '1', name: '机构客户'}, {id: '0', name: '个人客户'}];
            }
            if (!this.FOF) {
                this.initView.lpSource = [{id: '1', name: '机构客户'}, {id: '0', name: '个人客户'}];
            }
            this.isAuthPartnerEdit = this.isAuth ? isNoAuth(this.isAuth) : isNoAuth('已投资基金-合伙人认缴-编辑');
            this.partnerListAdd.fundPartnerEntity.fundId = this.itemId;
            this.getPartnerList();
        },
        mounted() {
            let fileTypeList = this.$store.state.user.fileFloderDicVoList;
            fileTypeList.map(item=>{
                if (item.name === '合伙人认缴') {
                    this.fileType = item.type;
                }
            })
        },
        methods: {
            //获取合伙人列表
            getPartnerList() {
                apiHttp(api.PARTNER_LIST,{fundId: this.itemId}).then( ret=>{
                    if (ret.resCode === STATUS_HTTP_SUCCESS) {
                        for ( let i of ret.data) {
                            i.show = false;
                            i.hide = true;
                            i.editStatus = false;
                            i.distHistoryList.map( item => {
                                item.userSourceArr = []
                            })
                        }
                        this.fundPartner = ret.data;
                    }
                })
            },
            //添加合伙人信息，弹出框
            addPartner() {
                this.dialogFormVisible = true;
                if (this.fundPartner[0]) {
                    let arr = []; // 合伙人历史
                    arr = arr.concat(this.fundPartner[0].distHistoryList);
                    arr.map(item=>{
                        item.id = '';
                        item.partnerId = '';
                        delete item.proportion;
                        delete item.existence;
                        apiHttp(api.GET_PARTNER_SOURCE,{fundState: item.partnerType}).then( ret=>{
                            if (ret.resCode === STATUS_HTTP_SUCCESS) {
                                item.userSourceArr = ret.data;
                            }
                        })
                    })
                    this.partnerListAdd = {
                        distHistoryList: [...arr],
                        fundPartnerEntity: {
                            fundId: this.itemId,
                            id: '',
                            reason: '',
                            recordingTime: '',
                        },
                        fileList: [],
                    }
                    this.fileDetail = [];
                } else {
                    this.partnerListAdd = {
                        distHistoryList: [{
                            attribute: '',  //资金属性
                            customerId: '', //合伙人Id
                            customerName: '',//合伙人name
                            holdingState: '',//持有状态 fundHolding
                            id: '',  //
                            life: '',//缴纳年限
                            paid: 0,//实缴金额
                            partnerId: '', //已设立基金合伙人主id
                            partnerType: null, //合伙人类型1 客户，2 母基金
                            stages: '', //出资分期
                            subscribed: '', //认缴金额
                            type: 0, //合伙人类型0 Lp 1 Gp
                            userSourceArr: [],//存放人的数组
                        }],
                        fundPartnerEntity: {
                            fundId: this.itemId,
                            id: '',
                            reason: '',
                            recordingTime: '',
                        },
                        fileList: [],
                    }
                    this.fileDetail = [];
                }
            },
            //弹出框的取消按钮
            cancelDialog() {
                this.dialogFormVisible = false;
            },
            //弹出框的确定按钮 保存合伙人信息接口
            addPartnerData() {
                if (!this.partnerListAdd.fundPartnerEntity.recordingTime) {
                    this.$message.error('请选择日期');
                    return false;
                }
                for (let i of this.partnerListAdd.distHistoryList) {
                    if (i.customerName === '') {
                        this.$message.error('请选择合伙人');
                        return false;
                    }
                }
                let addPartnerData = Object.assign({},this.partnerListAdd)
                addPartnerData.distHistoryList.map( item => {
                    delete item.userSourceArr
                })
                apiHttp(api.ADD_PARTNER, addPartnerData).then( ret=>{
                    if (ret.resCode === STATUS_HTTP_SUCCESS) {
                        this.$message({
                            message: '添加成功',
                            type: 'success',
                            center: true
                        });
                        this.refresh();
                        this.$emit('addRedDot', 1)
                        this.dialogFormVisible = false;
                        this.getPartnerList();
                    }
                })
            },
            //添加一行空的
            addPartnerTableData() {
                this.partnerListAdd.distHistoryList.push(Object.assign({},this.partnerList))
            },
            //删除表格中一行
            deletePartnerSingle(list,index) {
                if (index === -1) { //添加时候删除一行，直接截取
                    this.partnerListAdd.distHistoryList.splice(index, 1);
                } else {//修改时删除
                    this.$confirm('是否删除此条合伙人信息?', '提示', {
                        cancelButtonText: '取消',
                        confirmButtonText: '确定',
                        type: 'warning'
                    }).then(async () => {
                        apiHttp(api.DELETE_SINGLE_PARTNER,{id: list.id}).then( ret=>{
                            if (ret.resCode === STATUS_HTTP_SUCCESS) {
                                this.$message({
                                    type: 'success',
                                    message: '删除成功!',
                                    center: true
                                });
                                this.getPartnerList();
                            }
                        })
                    });
                }
            },
            //添加时候改变合伙人
            changePartner(index) {
                let arrId = this.partnerListAdd.distHistoryList[index].customerId;
                this.partnerListAdd.distHistoryList[index].userSourceArr.map( item=>{
                    if (arrId === item.id) {
                        this.partnerListAdd.distHistoryList[index].customerName = item.customerName;
                    }
                })
            },
            //合伙人展开收起
            partnerOpen(index) {
                if (this.fundPartner[index].show === false) {
                    this.fundPartner[index].show = true;
                    this.fundPartner[index].hide = false
                } else {
                    this.fundPartner[index].show = false;
                    this.fundPartner[index].hide = true
                }
            },
            //编辑单个合伙人信息
            editPartnerItem(row,index) {
                row.show = false;
                this.fundPartner[index].editStatus = true;
                this.editData = row;
                this.editIndex = index;
            },
            //取消编辑
            cancelEdit(index) {
                this.getPartnerList();
                this.fundPartner[index].show = true;
                this.fundPartner[index].editStatus = false;
            },
            //跳转合伙人
            goPartnerDetail(id,type) {
                if (type === '2') {
                    this.$router.push({name: 'invested-FOF', params: {itemId: id, tabIndex: '0', openWindow: true }});
                } else {
                    apiHttp(api.GET_GUEST,{guestId: id}).then( ret=>{
                        if (ret.resCode === STATUS_HTTP_SUCCESS) {
                            this.$router.push({name: 'client-management-info', params: {eClient_guestId: id, openWindow: true }});
                        }
                    })
                }
            },
            //根据来源选择不同的合伙人
            lpSourceChange(index) {
                this.partnerListAdd.distHistoryList[index].customerId = '';
                let value = this.partnerListAdd.distHistoryList[index].partnerType;
                apiHttp(api.GET_PARTNER_SOURCE,{fundState: value}).then( ret=>{
                    if (ret.resCode === STATUS_HTTP_SUCCESS) {
                        this.partnerListAdd.distHistoryList[index].userSourceArr = ret.data;
                    }
                })
            },
            //添加  文件成功返回的数据
            getFileUploadedReturnDataFun(arr) {
                arr.map(item=>{
                    this.partnerListAdd.fileList.push(item.id)
                })
                this.fileDetail = this.fileDetail.concat(arr)
            },
            //删除添加的一条文件
            deleteFileItem(index) {
                this.partnerListAdd.fileList.splice(index, 1);
                this.fileDetail.splice(index, 1);
            },
            addRedDot() {
                this.$emit('addRedDot',1)
            },
            disableFunction(item) {
                let partnerAuth = item.existence;//权限
                if (!item.existence) {//有权限
                    if (item.partnerType === '2') {
                        //母基金
                        partnerAuth = !isNoAuth('投资中母基金-母基金详情-查看');
                    } else {
                        partnerAuth = !isNoAuth('客户管理-客户信息-查看');
                    }
                }
                return partnerAuth;
            },
            refresh() {
                this.$emit('refresh')
            }
        },
        components: {
            'csUpload': require('__components/upload-btn/index'),//上传文件组件
            'csPartnerEdit': require('./edit.vue')
        },
    }
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
    .ca-title{
        height:42px;
    }
    table td{
        padding: 5px 5px;
        box-sizing: border-box;
    }
</style>
